// pages/learn/index.js
const apis = require('../../utils/api').apis;
import * as echarts from '../../ec-canvas/echarts.min.js';
let chartLine;

function getOption(data) {
  // console.log(data)
  var option = {
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      axisTick: {
        show: false //不显示坐标轴刻度线
      },
      axisLine: {
        show: false, //不显示坐标轴线
      },
      axisLabel: {
        show: true,
         textStyle: {
           color: '#888e93',  //更改坐标轴文字颜色
         }
      },
    },
    yAxis: {
      type: 'value',
      axisTick: {
        show: false //不显示坐标轴刻度线
      },
      axisLine: {
        show: false, //不显示坐标轴线
      },
      axisLabel: {
        show: true,
         textStyle: {
           color: '#888e93',  //更改坐标轴文字颜色
         }
      },
    },
    showSymbol: false,
    grid: {
      top: '15%', // 与容器顶部的距离
      bottom: '15%', // 与容器底部的距离
    },
    series: [{
      symbol: "none",
      smooth: true,
      data: data,
      type: 'line',
      itemStyle: {
        normal: {
          color: '#2ABD9C', //折点颜色
          lineStyle: {
            color: '#2ABD9C' //折线颜色
          }
        }
      },
      areaStyle: {
        normal: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: '#2ABD9C'
          }, {
            offset: 1,
            color: '#fff'
          }]),
        }
      },
    }]
  }
  // console.log(option)
  return option
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidenSty: false,
    dataArr: [],
    curricumlArr: [],
    todayCurricuml: [],
    dateWeek: [],
    certificate: [],
    planCurriculum: [],
    hour: null,
    min: null,
    weekActive: 0,
    daydate: 0,
    ecLine: {
      onInit: function (canvas, width, height,dpr) {
        //初始化echarts元素，绑定到全局变量，方便更改数据
        chartLine = echarts.init(canvas, null, {
          width: width,
          height: height,
           devicePixelRatio: dpr
        });
        canvas.setChart(chartLine);

        var option = getOption();
        chartLine.setOption(option);
      }
    },
    active: 1,
    myCurricuml: {
      isFree: "",
      isCurriculumSeries: "",
      pageNo: 1,
      pageSize: 10,
      total: 0,
    },
    userInfo:null,
  },
  getData() {
    var _this = this;
    apis.planQueryUserLearnTimeInThisWeek({}).then(res => {
      _this.setData({
        dataArr: res.data
      })
    })

  },
  switch (e) {
    let btn = e.currentTarget.dataset.type;
    console.log(btn)
    if (btn == 1) {
      this.setData({
        hidenSty: false
      })
    } else {
      this.setData({
        hidenSty: true
      })

    }
  },
  switchTab(e) {
    let type = e.currentTarget.dataset.type;
    // console.log(type)
    if (type == 'all') {
      this.setData({
        'myCurricuml.isFree': "",
        'myCurricuml.isCurriculumSeries': "",
        'myCurricuml.pageNo': 1,
        'myCurricuml.pageSize': 10,
        active: 1,
      })
    } else if (type == 'free') {
      this.setData({
        'myCurricuml.isFree': "userFree",
        'myCurricuml.isCurriculumSeries': "",
        'myCurricuml.pageNo': 1,
        'myCurricuml.pageSize': 10,
        active: 3,
      })
    } else if (type == 'yes') {
      this.setData({
        'myCurricuml.isFree': "",
        'myCurricuml.isCurriculumSeries': "yes",
        'myCurricuml.pageNo': 1,
        'myCurricuml.pageSize': 10,
        active: 2,
      })
    }
    this.getCurricuml()

  },
  getCurricuml() {
    wx.showLoading({
      title: '加载中',
    })
    apis.curriculumQueryUserCurriculum({
      isFree: this.data.myCurricuml.isFree,
      curriculumForm: this.data.myCurricuml.curriculumForm,
      isCurriculumSeries: this.data.myCurricuml.isCurriculumSeries,
      pageNo: this.data.myCurricuml.pageNo,
      pageSize: this.data.myCurricuml.pageSize
    }).then(res => {
      if (this.data.myCurricuml.pageNo == 1) {
        this.setData({
          curricumlArr: res.data.records,
          'myCurricuml.total': res.data.total
        })
      } else {
        this.setData({
          curricumlArr: this.data.curricumlArr.concat(res.data.records),
          'myCurricuml.total': res.data.total
        })
      }
      wx.hideLoading()
      // console.log(this.data.curricumlArr)
    })
  },
  // 获取学习日历
  getDate(d) {
    apis.planQueryUserLearnStateOnThisWeek({}).then(res => {
      if(res.success){
        this.setData({
          dateWeek: res.data
        })
        res.data.map((item, index) => {
          if (item.dateState == 'today') {
            this.setData({
              weekActive: index,
              daydate: item.date
            })
            this.getTodayCurricuml()
          }
        })
      }
    })
  },
  // 获取今日学习课程
  getTodayCurricuml() {
    apis.planQueryUserLearnCurriculumToday({
      date: this.data.daydate
    }).then(res => {
      this.setData({
        todayCurricuml: res.data
      })
    })
  },
  // 获取目标证书
  getCertificate() {
    apis.planQueryGoalCertificate({}).then(res => {
      this.setData({
        certificate: res.data
      })
    })
  },
  // 获取计划页面课程
  getPlanCurriculum() {
    apis.curriculumQueryPlanCurriculum({}).then(res => {
      this.setData({
        planCurriculum: res.data
      })
    })
  },
  // 点击日期
  switchWeek(e) {
    let index = e.currentTarget.dataset.index;
    let date = e.currentTarget.dataset.date;
    this.setData({
      weekActive: index,
      daydate: date
    })
    this.getTodayCurricuml()
  },
  goDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/curriculum/pages/course/index?id=' + id,
    })
  },
  changeCurrcuiml() {
    wx.navigateTo({
      url: '../../learn/pages/change_plan/index',
    })
  },
  openVip(){
    let token=wx.getStorageSync('Xd_Token');
    if(token==null){
      wx.showToast({
        title: '请您先登陆',
        icon:'none'
      })
      return
    }
    wx.reLaunch({
      url: '/vip/pages/vip_buy/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let pages = getCurrentPages()
    // console.log('学习',pages)
    // // if (pages.length >= 1) {
    //   let prevpage = pages[pages.length - 1]
    //   console.log(prevpage.route)
    //   if(prevpage.route=='pages/learn/index'){
    //     wx.switchTab({
    //       url: '/pages/index/index',
    //     })
    //   }
  // }
    // this.getDate();
    // this.getCertificate();
    // this.getPlanCurriculum()
    // this.getData();
    // this.getCurricuml();
    // console.log(1)
    // wx.setStorageSync('Xd_Token', 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxNTY1MjUxMjkwOCJ9.sADt8UYwkE6tOLNmDUOqpZEpH-1NCiI7kNreA-wwAu4')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // var data = [40, 50, 30, 20, 10, 0, 28]
    // setTimeout(() => {
    //   var data = this.data.dataArr.weekLearnTimeAnyDay
    //   var option = getOption(data);
    //   chartLine.setOption(option);
    // }, 1000)
    // console.log(2)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var token=wx.getStorageSync('Xd_Token');

    if(token==''||token==undefined||token==null){
      wx.redirectTo({
        url: '/login/pages/login/index',
      })
      return
    }
    var userInfo=wx.getStorageSync('userInfo');
    this.setData({
      userInfo
    })
    console.log(userInfo)
    this.getDate();
    this.getCertificate();
    this.getPlanCurriculum()
    this.getData();
    this.getCurricuml();
    setTimeout(() => {
      var data = this.data.dataArr.weekLearnTimeAnyDay
      var option = getOption(data);
      chartLine.setOption(option);
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.curricumlArr.length < this.data.myCurricuml.total && this.data.hidenSty == false) {
      console.log('上拉加载')
      let current = this.data.myCurricuml.pageNo;
      ++current
      this.setData({
        'myCurricuml.pageNo': current
      })
      this.getCurricuml()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})